package com.atguigu.gulimall.product.config;

import com.atguigu.gulimall.product.quartz.QuartzJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * Quartz配置类
 */
@Configuration
public class QuartzConfig {

    //秒	   分    时	每月第几天(?)   月       每周第几天(?)  （年）
    private static final String CRON_EXPRESSION_3SECONDS = "0 0 0 * * ? ";//每天凌晨0:00:00执行一次,?用于无指定日期

    /**
     * 1、创建Job对象
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //关联我们自己的Job类
        factoryBean.setJobClass(QuartzJob.class);
        return factoryBean;
    }

    /**
     * 2、创建Trigger对象
     */
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean factoryBean=new CronTriggerFactoryBean();
        //关联JobDetail对象
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        //设置触发时间
        //factoryBean.setCronExpression("0/2 * * * * ?");  //每2秒触发一次， 分钟，小时，天，月，星期
        factoryBean.setCronExpression(CRON_EXPRESSION_3SECONDS);  //在每天0-22点期间的每1分钟触发
        return factoryBean;
    }
    /**
     * 3、创建Scheduler
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cornTriggerFactoryBean){
        SchedulerFactoryBean factoryBean=new SchedulerFactoryBean();
        //关联trigger
        factoryBean.setTriggers(cornTriggerFactoryBean.getObject());
        return factoryBean;
    }
}